package day5.homework.StudentSystem;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Test {
    static Scanner sc = new Scanner(System.in);
    //定义接受学生信息集合
    static Map<String,Student> students = new HashMap();
    //初始化
    static {
        input();
    }
    //Map.Entry(key和value)
    //jdk1.8的Stream流的方法
    //map.entrySet()  //获取map集合的所有entry对象(包含key和value)
    //        .stream() //把Entry对象的集合 存入数据流中
    //        .filter((Map.Entry<String,Student> e)->
    //                e.getKey().contains(no)) //过滤器 判断哪些数据不要
    //        .forEach((Map.Entry e)->
    //        System.out.println(e.getKey()+" "+e.getValue())); //遍历的方法
    public static void main(String[] args) {
        menu();
        System.out.println("已退出学生成绩管理系统");
    }
    public static void menu(){
        while (true){
            System.out.println("---------------------");
            System.out.println("---学生成绩管理系统---");
            System.out.println("---------------------");
            System.out.println("1.查询学生信息");
            System.out.println("2.新增学生信息");
            System.out.println("3.批量删除学生信息");
            System.out.println("4.修改学生信息");
            System.out.println("5.退出系统");
            System.out.println("---------------------");
            System.out.println("请输入你的选择:");
            int i = sc.nextInt();
            switch (i){
                case 1:inquireInfo();break;
                case 2:addStudent();break;
                case 3:deleteStudent();break;
                case 4:changeInfo();break;
                case 5:return;
            }
        }
    }
    //查询
    public static void inquireInfo(){
        System.out.println("请选择查询的方式:");
        System.out.println("1.查询所有信息 2.模糊查询");
        int i = sc.nextInt();
        switch (i){
            case 1:allInquire();break;
            case 2:dimInquire();break;
        }
    }
    //查询所有
    public static void allInquire(){
        for (Map.Entry<String,Student> e : students.entrySet()){
            System.out.println("学号:" + e.getValue().studentId + ",姓名:" + e.getValue().name
                        + ",学科:" + e.getValue().subject + ",成绩:" + e.getValue().score);
        }
    }
    //模糊查询
    public static void dimInquire(){
        System.out.println("请输入查询的学号:");
        String id = sc.next();
        for (Map.Entry<String,Student> e : students.entrySet()){
            if (e.getKey().contains(id)){
                System.out.println("学号:"+e.getValue().studentId+",姓名:"+e.getValue().name
                        +",学科:"+e.getValue().subject +",成绩:"+e.getValue().score);
            }else System.out.println("未为查询到学生信息!");
        }
    }
    //新增
    public static void addStudent(){
        System.out.println("请输入学号：");
        String studentId = sc.next();
        System.out.println("请输入姓名：");
        String name = sc.next();
        System.out.println("请输入学科：");
        String subject = sc.next();
        System.out.println("请输入成绩：");
        double score = sc.nextDouble();
        students.put(studentId,new Student(studentId,name,subject,score));
        System.out.println(studentId+"添加成功!");
        output();
    }
    //删除
    public static void deleteStudent(){
        System.out.println("请输入需要删除的学生的学号：");
        String Ids = sc.next();
        String[] deleteIds = Ids.split(",");
        for (String s:deleteIds){
            if (students.containsKey(s)){
                students.remove(s);
            }
        }
        output();
    }
    //修改
    public static void changeInfo(){
        System.out.println("请输入需要修改的学生的学号:");
        String id = sc.next();
        if (students.containsKey(id)){
            System.out.println("请输入需要修改的属性名:");
            String info = sc.next();
            System.out.println("请输入修改后的属性值:");
            String value = sc.next();
            String[] fields = info.split(",");
            String[] values = value.split(",");
            for (int i = 0; i < fields.length; i++) {
                if (fields[i].equals("studentId")){
                    students.get(id).studentId=values[i];
                }else if (fields[i].equals("name")){
                    students.get(id).name=values[i];
                }else if (fields[i].equals("subject")){
                    students.get(id).subject=values[i];
                }else if (fields[i].equals("score")){
                    students.get(id).score=Double.parseDouble(values[i]);
                }else System.out.println("该属性名不存在!");
            }
        }
    }
    //序列化
    public static void output(){
        try (FileOutputStream fos = new FileOutputStream("d://IO//a//student.xxx");
             ObjectOutputStream oos = new ObjectOutputStream(fos);
        ){
            oos.writeObject(students);
            System.out.println("学生信息写入本地成功!");
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    //反序列化
    public static void input(){
        try(FileInputStream fis = new FileInputStream("d://IO//a//student.xxx");
            ObjectInputStream ois = new ObjectInputStream(fis);
        ) {
            students = (Map<String, Student>) ois.readObject();
        }catch (FileNotFoundException e){
            System.out.println("欢迎第一次使用！！！");
        } catch (IOException e){
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
